package main

import (
  "flag"
  "fmt"
  "math/rand"
  "time"
)

var (
  length int
  charset string
)

const (
  NumStr = "0123456789"
  CharStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  SpecStr = "~!@#$%^&*()_+"
)

func parseArgs(){
  flag.IntVar(&length, "l",16,"-l 指定生成密码的长度")
  flag.StringVar(&charset,"t","num",
    `-t 指定密码生成的字符集,
num:只使用数字[0-9],
char:只使用英文字母[a-zA-Z],
mix: 使用数字和字母,
advance:使用数字、字母以及特殊字符`)
  flag.Parse()
}

func generatePasswd() string{
/*
   // byte is an alias for uint8 and is equivalent to uint8 in all ways. It is
   // used, by convention, to distinguish byte values from 8-bit unsigned
   // integer values.
   type byte = uint8
*/
  var passwd []byte = make([]byte,length,length)
  var sourceStr string

  if charset == "num"{
    sourceStr = NumStr
  }else if charset == "char" {
    sourceStr = CharStr
  }else if charset == "mix" {
    sourceStr = fmt.Sprintf("%s%s",NumStr,CharStr)
  }else if charset == "advance" {
    sourceStr = fmt.Sprintf("%s%s%s",NumStr,CharStr,SpecStr)
  }else {
    sourceStr = NumStr
  }

  fmt.Println("source:",sourceStr)

  for i := 0; i <length; i++ {
    index := rand.Intn(len(sourceStr))
    passwd[i] = sourceStr[index]
  }

  return string(passwd) //fdLVHRBDKlmgcTGkInVqvtlxpckheuRY
}

func main() {
  parseArgs()
  fmt.Printf("length:%d charset:%s\n",length,charset)

  rand.Seed(time.Now().UnixNano())
  passwd := generatePasswd()
  fmt.Println(passwd)
}
